package com.macrog.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.macrog.rabbitmq.utils.ConnectionFactoryUtil;

import java.io.IOException;

/**
 * Created by Administrator on 2018/9/2.
 */
public class Send {

    private static final String EXCHANGE_NAME = "topicExchange";

    private static final String[] routingKeys = { "key.1","key.1.2", "abc.1", "abc.1.2" };

    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionFactoryUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String message = "topic模式消息";
//        channel.basicPublish(EXCHANGE_NAME,"abc",null,message.getBytes());

        //分别推送"adtec.laoliu","ali.xiaowang", "tencent.laozhang", "adtec.xiaohu"消息
        for (String routingKey : routingKeys) {
            String msg = message+ "' from "+routingKey;
            channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
            System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
        }

        ConnectionFactoryUtil.release(connection,channel);
    }
}
